package com.ellen.mac.chatdemo.fragment;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avos.avoscloud.im.v2.AVIMConversation;
import com.ellen.mac.chatdemo.R;
import com.ellen.mac.chatdemo.adpter.CommonListAdapter;
import com.ellen.mac.chatdemo.cache.ACache;
import com.ellen.mac.chatdemo.viewholder.ConversationItemHolder;
import com.ellen.mac.chatdemo.adpter.ListConversationAdpter;
import com.ellen.mac.chatdemo.cache.ConversationItemCache;
import com.ellen.mac.chatdemo.event.ConversationItemLongClickEvent;
import com.ellen.mac.chatdemo.event.TypeMessageEvent;
import com.ellen.mac.chatdemo.event.OfflineMessageCountChangeEvent;
import com.ellen.mac.chatdemo.utils.ChatKit;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mac on 2017/12/2.
 * 所有会话列表页
 */
public class ConversationListFragment extends Fragment {
    protected SwipeRefreshLayout refreshLayout;
    protected RecyclerView recyclerView;

    protected CommonListAdapter<AVIMConversation> itemAdapter;
    protected LinearLayoutManager layoutManager;
    ACache mACache;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.contact_fragment, container, false);

        refreshLayout =  view.findViewById(R.id.contact_fragment_srl_list);
        recyclerView =  view.findViewById(R.id.contact_fragment_rv_list);

        refreshLayout.setEnabled(false);
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        itemAdapter = new CommonListAdapter<>(ConversationItemHolder.class);
        recyclerView.setAdapter(itemAdapter);

        mACache = ACache.get(getActivity());

        EventBus.getDefault().register(this);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        updateConversationList();
    }

    @Override
    public void onResume() {
        super.onResume();
        updateConversationList();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 收到对方消息时响应此事件
     *
     * @param event
     */
    @Subscribe
    public void onEvent(TypeMessageEvent event) {
        updateConversationList();
    }

    /**
     * 删除会话列表中的某个 item
     *
     * @param event
     */
    @Subscribe
    public void onEvent(ConversationItemLongClickEvent event) {
        if (null != event.conversation) {
            String conversationId = event.conversation.getConversationId();
            ConversationItemCache.getInstance().deleteConversation(conversationId);
            //todo Cache
            mACache.remove(conversationId);
            updateConversationList();
        }
    }

    /**
     * 刷新页面
     */
    private void updateConversationList() {
        List<String> convIdList = ConversationItemCache.getInstance().getSortedConversationList();
        //todo Cache
        List<AVIMConversation> conversationList = new ArrayList<>();
        for (String convId : convIdList) {
            conversationList.add(ChatKit.getInstance().getClient().getConversation(convId));
        }

        itemAdapter.setDataList(conversationList);
        itemAdapter.notifyDataSetChanged();

    }

    /**
     * 离线消息数量发生变化是响应此事件
     * 避免登陆后先进入此页面，然后才收到离线消息数量的通知导致的页面不刷新的问题
     *
     * @param updateEvent
     */
    @Subscribe
    public void onEvent(OfflineMessageCountChangeEvent updateEvent) {
        updateConversationList();
    }
}
